<template>
  <div>
    <vCommonPage :title="$route.query.pageTitle">
      <div slot="customContent">
        <!-- start:自定义页面内容 -->
        <el-row :gutter="20" v-if="this.cardType=='0'">
          <el-col :span="sceneid==='sstbg'?'12':''" v-for="(item, index) in operationalMatters" :key="index">
            <el-image
              style="width: 303px; height: 143px"
              :src="item.loginIcon"
              fit="fill"
              @click="waitingReadcard(item)"
            ></el-image>
          </el-col>
        </el-row>
        <!-- end:自定义页面内容 -->
        <Ynkdk v-if="this.cardType=='1'" @ReadedCard="ReadedCard" :loginUrl="loginUrl"></Ynkdk>
        <Ynkdkhd v-if="this.cardType=='101'" @ReadedCard="ReadedCard" :loginUrl="loginUrl"></Ynkdkhd>
        <Sbkdk v-if="this.cardType=='2'" @ReadedSbkCard="ReadedSbkCard" :loginUrl="loginUrl"></Sbkdk>
        <Sfzdk v-if="this.cardType=='3'" @ReadedIdInfo="ReadedIdInfo" :loginUrl="loginUrl"></Sfzdk>
        <Sfzdkhd v-if="this.cardType=='301'" @ReadedIdInfo="ReadedIdInfo" :loginUrl="loginUrl"></Sfzdkhd>  
        <Dzjkk v-if="this.cardType=='4'" @ReadedCard="ReadedCard" :loginUrl="loginUrl"></Dzjkk>
        <Ynewm v-if="this.cardType=='7'" @ReadedCard="ReadedCard" :loginUrl="loginUrl"></Ynewm>
        <Reguser v-if="this.showRegModel==true" :cardType="cardType" v-bind:miCardInfo="this.miCardInfo" @RegedSbkCard="RegedSbkCard" @regPatient="regPatient"></Reguser>
      </div>
    </vCommonPage>
  </div>
</template>

<script>
// 业务事项的图标
import DzjkkUrl from "@images/readcard/dzjkk.png";
import SbkUrl from "@images/readcard/sbk.png";
import SfzUrl from "@images/readcard/sfz.png";
import YnkUrl from "@images/readcard/ynk.png";
import YnewmUrl from "@images/readcard/ynewm.png";
import readingYnk from "@images/readcard/reading01.png";
import readingSbk from "@images/readcard/reading03.png";
import readingSfz from "@images/readcard/reading01.png";
import readingDzjkk from "@images/readcard/reading01.png";
import readingYnewm from "@images/readcard/reading04.png";
import { readCardDetail,commonPost } from "@api/data";
import Dzjkk from './dzjkk.vue';
import Ynewm from './ynewm.vue';  // 院内二维码
import Sfzdk from './sfzdk.vue';
import Ynkdk from './ynkdk.vue';
import Ynkdkhd from './ynkdkhd.vue';//hd 华大四合一读卡器
import Sbkdk from './sbkdk.vue';
import Sfzdkhd from './sfzdkhd.vue';
import Reguser from './reguser.vue'
export default {
  name: "readCard",
  components: {
    Dzjkk,
    Ynewm,
    Sfzdk,
    Ynkdk,
    Ynkdkhd,
    Sbkdk,
    Sfzdkhd,
    Reguser
  },
  data() {
    return {
      cmiid:this.$store.state.terminfo.orginfo.cmiid,
      loginUrl:"",
      userinfo: [],
      idinfo:[],
      ybinfo:[],
      cardType: "0",
      cardNo: "",
      idNo:"",
      name:"",
      miCardInfo:"",
      readingUrl: "",
      showWaitingModal: false,
      showRegModel:false,
      sceneid:this.$store.state.terminfo.seceninfo.sceneid,
      operationalMatters: this.$route.query.logintype,
      //  [
      //   //业务事项配置
      //   // {
      //   //   id: "ynk",
      //   //   cardId: "1",
      //   //   name: "院内就诊卡",
      //   //   iconUrl: YnkUrl,
      //   //   reading: readingYnk
      //   // },
      //   {
      //     id: "sbk",
      //     cardId: "2",
      //     name: "社保卡",
      //     iconUrl: SbkUrl,
      //     reading: readingSbk
      //   },
      //   {
      //     id: "sfz",
      //     cardId: "3",
      //     name: "身份证",
      //     iconUrl: SfzUrl,
      //     reading: readingSfz
      //   },
      //   {
      //     id: "jkk",
      //     cardId: "4",
      //     name: "电子健康卡",
      //     iconUrl: DzjkkUrl,
      //     reading: readingDzjkk
      //   },
      //   {
      //     id: "ynewm",
      //     cardId: "1",
      //     name: "院内二维码",
      //     iconUrl: YnkUrl,
      //     reading: readingDzjkk
      //   }
      // ]
    };
  },
  methods: {
    //就诊卡读卡完成接收
    ReadedCard(data){
      this.cardNo=data;
      this.queryuserinfo();
    },
    //医保注册完成
      RegedSbkCard(data){
      this.showRegModel=false;
      var	ybcardno = data[5];
      this.cardNo=ybcardno;
      this.queryuserinfo();
    },
    //医保卡读卡完成
      ReadedSbkCard(data){
        this.miCardInfo=data;
        let orginfo = this.$store.state.terminfo.orginfo;
          if(orginfo.cmiid==='104'){
            this.SbkCardStepYj(data);
          }
        else{
          this.SbkCardStep2(data);
        }
    },
    //医保卡读卡完成后延吉处理
    SbkCardStepYj(data){
          var outStr = data.split("^")[2];
          var outParams = outStr.split("|");
          this.ybinfo=outParams;
          //医保读卡返回信息
          this.$store.dispatch("user/updatemiinfo", outParams);
          // var ybgrbh = outParams[0];
          // var ybdwbh = outParams[1];
          // var idNo = outParams[2];
          // var name = outParams[3];
          // var sex = outParams[4];
          // var minzu = outParams[5];
          // var birthday = outParams[6];
           this.idNo=outParams[2];
           this.name=outParams[3];
           this.cardNo=outParams[7];
           this.queryuserinfo();
      },
      //医保卡读卡完成后九台/长春市处理
    SbkCardStep2(data){
         var	ybcardno ='';
          var outStr = data.split("^")[2];
          var outParams = outStr.split("|");
          this.$store.dispatch("user/updatemiinfo", outParams);
          this.ybinfo=outParams;
          this.idNo=outParams[1];
          this.name=outParams[2];
          this.cardNo=outParams[5];

          ybcardno=outParams[5];
          var gwybz = outParams[9];//1公务员，0非公务员
          if(parseInt(gwybz)==1){
            this.$confirm('您的医保类型是公务员，请去窗口审批后使用，否则可能不享受公务员待遇,是否继续?', '提示', {
            confirmButtonText: '继续操作',
            cancelButtonText: '去窗口',})
                .then(_ => {
              this.queryuserinfo();
                })
                .catch(_ => {
                this.$router.push({ name: "home" });
              });
          }
          else{
            this.queryuserinfo();
          }
            // var csrq00 = outParams[4];
            // var grbh00 = outParams[0];
            // var xbie00 = outParams[3];
            // var ybzhye = outParams[16];
            // var sfzhao = outParams[1];
            // var ybrylb = outParams[8];
            // var ybdwbh = outParams[7];

    },
    ReadedIdInfo(data){
      console.log("身份证信息=", data);
      this.miCardInfo = data;
      this.miCardInfo.isSfz = true;
      this.idinfo = data;


      this.name = data.Name,
      this.cardNo = data.ID,
      // this.idNo = data,
      this.queryuserinfo();
    },
    waitingReadcard(item) {
      this.loginUrl=item.loginUrl;
      console.log("loginUrl="+this.loginUrl);
       this.cardType=item.cardId;

      // let orginfo = this.$store.state.terminfo.orginfo
      // console.log("orginfo",orginfo,"item",item)
      //  if(orginfo.cmiid==='104'){
      //     this.cardType=item.cardId;
      //     // if (item.cardId == "1" ) {  //||item.cardId == "3"
      //     //   this.cardType='0';
      //     //   this.$alert("请使用医保卡和电子健康卡。其他卡类型暂未开通。");
      //     // } 
      //     // else{
      //     // this.cardType=item.cardId;
      //     // }
      //  }else if(orginfo.cmiid==='1596'){
      //       if (item.cardId == "3") {
      //       this.cardType='0';
      //       this.$alert("请使用电子健康卡、就诊卡和医保卡。其他卡类型暂未开通。");
      //     } 
      //     else{
      //     this.cardType=item.cardId;
      //     }
      //  }else if(orginfo.cmiid==='105'){//暂定集安105，集安无医保   ||item.cardId == "4"
      //     if(this.$route.query.id === "mzgh"){
      //         if (item.cardId == "1"||item.cardId == "2"  ||item.cardId == "4" ) {
      //           this.cardType='0';
      //           this.$alert("请使用身份证挂号。其他卡类型暂未开通。")
      //         } 
      //         else{
      //           this.cardType=item.cardId;
      //         }
      //     }else  {
      //       if (item.cardId == "1"||item.cardId == "2") {
      //         this.cardType='0';
      //         this.$alert("请使用身份证挂号。其他卡类型暂未开通。");
      //       } 
      //       else{
      //         this.cardType=item.cardId;
      //       }
      //     }
      //  }
      // //  this.cardType=item.cardId;

    },

    nextStep() {
      this.showWaitingModal = false;
      let routeName = this.$route.query.id; // 定义路由跳转的路径名变量
      // this.queryuserinfo();
      let pageTitle = ""; // 定义路由跳转的新页面名称传至下一个页面
      switch (routeName) {
        case "mzgh":
          routeName = "mzgh";
          pageTitle = "预约挂号";
          break;
        case "mzyy":
          routeName = "mzyy";
          pageTitle = "请选择门诊预约";
          break;
        case "mzjs":
          routeName = "mzjs";
          pageTitle = "门诊缴费";
          break;
        case "mzcz":
          routeName = "mzcz";
          pageTitle = "请选择门诊充值";
          break;
      }
      this.$router.push({
        name: routeName,
        query: { id: this.cardId, pageTitle: pageTitle }
      });
    },
    updatepatinfo(){
       let data = {};
      let userinfo=this.$store.state.user.userinfo;
      data.url = "/api/UpdatePatientInfo";
      data.payload = {
        patientId: userinfo.patientId,
        name: this.name,
        idNo:this.idNo,
        cardNo:this.idNo,
        updateType:"0",
        ssCardNo: this.cardNo,
        ssPersonaldNo: this.ybinfo[0],
        miCardInfo: this.miCardInfo,
        chargeType: "02",
        chargeType01: this.ybinfo[8],
        termId: this.$store.state.terminfo.terminfo.termId,
        hisOperNo:this.$store.state.terminfo.terminfo.hisOperNo
      };
      commonPost(data).then(res => {//更新完成后重新获取个人信息
        this.queryuserinfo();
      });
    },
    queryuserinfo() {
      // this.$store.dispatch("user/updateuserinfo", this.userinfo);
      let data = {};
	  //获取病人基本信息
      data.url = "/api/GetPatientInfo";
      data.payload = {
        name: this.name, //姓名
        cardType: this.cardType, //这里要拿到对应的刷卡类型（1,院内卡2社保卡3身份证4电子健康卡）
        cardNo: this.cardNo, //卡号
        idNo: this.idNo,  //证件号码
        hisOperNo:this.$store.state.terminfo.terminfo.hisOperNo, //his操作员号
        termId:this.$store.state.terminfo.terminfo.termId //终端编号
      };
      readCardDetail(data).then(res => {
        // this.userinfo = res.data;
        console.log("readCardDetail",res)
        res.data.map(item => {
          this.userinfo = item;
          this.userinfo.cardType=this.cardType;
          this.$store.dispatch("user/updateuserinfo", this.userinfo);
          var chargeType=item.chargeType;
          let routeName = this.$route.query.id;
          if(routeName =='mzjs'){
             if(this.cardType ==="2"){
               if(parseInt(chargeType)!=3){
                  this.$notify({
                  title: '抱歉',
                  type:'error',
                  duration:8000,
                  message: "自费患者无法用医保卡门诊缴费"});
               }
               else{
               this.nextStep();
               }
             }
             else{
                  if(parseInt(chargeType)==3){
                  this.$notify({
                  title: '抱歉',
                  type:'error',
                  duration:8000,
                  message: "医保患者请使用医保卡门诊缴费"});
               }
              else{
               this.nextStep();
               }
             }
          }

          else  if(routeName =='mzgh'||routeName =='mzyy'){
          if(this.cardType ==="2"){//读医保卡操;作
            if(parseInt(chargeType)!=3){//患者费用类别不是医保，提示做自费转医保
              this.$confirm('您的在本院不是医保患者,继续操作不能医保报销，请选择?', '提示', {
                    confirmButtonText: '继续自费操作',
                    cancelButtonText: '自费转医保',})
                        .then(_ => {
                         this.nextStep();
                        })
                        .catch(_ => {
                        this.updatepatinfo();
                      });
            }
            else{
               this.nextStep();
            }
          }
          else{//非医保卡，获取到医保病人
            if(parseInt(chargeType)===3){
              //this.$alert("您在本院是医保患者，请使用医保卡或去窗口修改为自费患者。");
              this.$confirm('您的在本院是医保患者,继续操作不能医保报销，请选择?','提示', {
                cancelButtonText: '取消操作',
                confirmButtonText: '医保转自费'})
              .then(_ => {
                let data = {};
                let userinfo=this.$store.state.user.userinfo;
                data.url = "/api/UpdatePatientInfo";
                data.payload = {
                  patientId: userinfo.patientId,
                  name: userinfo.name,
                  idNo:userinfo.idNo,
                  ssCardNo: userinfo.ssCardNo,
                  chargeType: userinfo.chargeType,
                  updateType: 1,//医保转自费
                  termId: this.$store.state.terminfo.terminfo.termId,
                  hisOperNo:this.$store.state.terminfo.terminfo.hisOperNo,
                };
                commonPost(data).then(res => {
                  // this.nextStep();
                  this.queryuserinfo();//转成功后再次获取患者信息。
                });
              })
              .catch(_ => {
                this.cardType='0';
              });
            }
            else{//其余读卡后直接下一步（可能存在医保转自费）
              this.nextStep();
            }
            }
           }
           else{
              this.nextStep();
           }
        }
      );
      }).catch(e=>{
        console.log("读卡失败",e);
        if(this.cardType==='3'){
          if(this.$route.query.id === "zzfk"){
            return 
          }else{
            // this.regPatient()
            this.showRegModel = true
          }  
        }else if(this.cardType==='2'){
          this.showRegModel=true;
        }
    }
    );
    },
    regPatient(phone) {
      this.showRegModel=false;
      console.log("regPatient",this.idinfo)
      let data = {}
      data.url = "/api/RegPatientInfo"
      data.payload = {
        name: this.idinfo.Name,
        sex:  this.idinfo.Sex,
        birthDate: this.idinfo.Birthday,
        phone: phone,
        address: this.idinfo.Address,
        cardNo: this.idinfo.ID,
        cardType: "3",
        nation: this.idinfo.Nation,
        idType: "01",
        idNo:this.idinfo.ID,
        termId: this.$store.state.terminfo.terminfo.termId,
        hisOperNo: this.$store.state.terminfo.terminfo.hisOperNo
      };
      commonPost(data).then(res => {
        console.log("regPatient",res)
        // let url = res.data.qrcode;
        // this.patientId = res.data.patientId;
        // 电子健康卡注册完成后转换病人性质为医保
        // this.updatepatinfo()
        this.queryuserinfo()
        // this.$emit("RegedSbkCard", this.ybinfo);
      });
    },
  },
  mounted() {
    let logintype = this.$route.query.logintype; // 定义路由跳转的路径名变量
      console.log("logintype=", logintype);

  }
};
</script>

<style scoped>
.el-row {
  /* padding: 0 120px; */
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.el-col {
  position: relative;
  box-sizing: border-box;
  margin: 30px 0;
  text-align: center;
}
</style>
<style>
.el-form-item__content {
  float: left;
}
</style>
